package sm;

import static org.junit.Assert.*;
import java.lang.Integer;

import java.math.BigInteger;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class PurchaseTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	
	@Before
	public void setUp() throws Exception {
		
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testServices(){
		TempService.getInstance();
		assertEquals(2,TempService.getInstance().getEventId());
		assertEquals(1,ServiceRegistry.getSizeServiceIds());
		assertTrue(ServiceRegistry.isServiceRegistered("2"));
		assertTrue(ServiceRegistry.serviceIds.containsKey("2"));
		assertFalse(ServiceRegistry.isServiceRegistered("3"));
		Integer one = (Integer)ServiceRegistry.serviceIds.get("2");
		assertEquals(2,one.intValue());
		assertEquals(2,((Integer)ServiceRegistry.serviceIds.get("2")).intValue());
		TempService temp1 = TempService.getInstance();
		TempService temp2 = TempService.getInstance();
		assertEquals(1,ServiceRegistry.serviceIds.size());
		
		LightService light1 = LightService.getInstance();
		assertEquals(3,light1.getEventId());
		assertEquals(50,light1.getSampleFreq());
				
		LightService.getInstance();
		assertEquals(2,ServiceRegistry.serviceIds.size());
		assertEquals(2,((Integer)ServiceRegistry.serviceIds.get("2")).intValue());
		assertEquals(3,((Integer)ServiceRegistry.serviceIds.get("3")).intValue());
		
		byte[] eventByte = "1".getBytes();		
		System.out.println(eventByte);
		char[] eventChar = "1".toCharArray();
		System.out.println(eventChar);
		String suerte = "2";
		char[] eventChar2 = suerte.toCharArray();
		System.out.println(eventChar2);
		char eventChar3 = eventChar2[0];
		assertEquals('2',eventChar3);
		
		EventSelector eventSelector = new EventSelector();
		SensingService eventObject;
		eventObject = eventSelector.getEventObject("3");
		assertEquals(3, eventObject.getEventId());
		eventObject = eventSelector.getEventObject("2");
		assertEquals(2, eventObject.getEventId());
		assertEquals(55, eventObject.getSampleFreq());
		//assertEquals(TempService.getInstance(),eventObject.getInstance());
		
		
		LightService light2 = LightService.getInstance();
		SensingService light3 = LightService.getInstance();
		assertEquals(3, light2.getEventId());
		assertEquals(3, light3.getEventId());
		assertEquals(LightService.getInstance(),light2.getInstance());
		
		assertEquals(light1,eventSelector.getEventObject("3"));
		//assertEquals(SensingService.getInstance(), eventSelector.getEventObject("2"));
		assertEquals(TempService.getInstance(), eventSelector.getEventObject("2"));
		
		assertTrue(ServiceRegistry.isServiceRegistered("3"));
		System.out.println(ServiceRegistry.getInstance().serviceIds.toString());
		//ServiceRegistry.serviceIds.remove("2"); quita la llave pero como ya hay un instance nunca 
		// crea otro entonces nunca se puede volver a registrar un servicio.
		
				
	}
	@Test
	public void testServicesExtra(){
		long time1 = System.currentTimeMillis();
		Long long1 = new Long(System.currentTimeMillis());
		System.out.println("long1" + long1);
		Date date1 = new Date();
		System.out.println("date   " + date1.getTime());
		Long long2 = new Long(time1);
		Long long3 = new Long(time1);
		assertTrue(long2.equals(long3));
		System.out.println("long2-3  " + long2 + " " + long3);
		long duration = 5;
		long currentTime = System.currentTimeMillis();
		long endTime = currentTime + duration;
		System.out.println("current " + currentTime + " end " + endTime + " dur " + duration);
	}
	@Test
	public void testTempService(){
		LightService.getInstance();
		assertTrue(ServiceRegistry.serviceIds.containsKey("3"));
		TempService.getInstance();
		assertTrue(ServiceRegistry.isServiceRegistered("2"));
		//assertTrue(ServiceRegistry.serviceIds.containsKey("2"));
		
	}
	@Test
	public void testServiceManager(){
		ServiceManager serviceManager = ServiceManager.getInstance();
		System.out.println(ServiceRegistry.getInstance().serviceIds.toString());
		assertEquals(3,ServiceRegistry.serviceIds.size());
		//ServiceRegistry.getInstance().registerService("2", 2);
		assertTrue(ServiceRegistry.serviceIds.containsKey("2"));
		assertTrue(ServiceRegistry.serviceIds.containsKey("3"));
		assertTrue(ServiceRegistry.serviceIds.containsKey("1000"));
		assertEquals(2,((Integer)ServiceRegistry.serviceIds.get("2")).intValue());
		assertEquals(3,((Integer)ServiceRegistry.serviceIds.get("3")).intValue());
		
		
	}

}
